import user from '../../net/user'

// initial state
// shape: [{ id, quantity }]
const state = {
  userInfo: {
    name: '',
    mobile: '',
  },
  checkoutStatus: null,
}

// getters
const getters = {}

// actions
const actions = {
  initUserInfo ({commit, state}, callback) {
    // user.cleanUserInfoLocal()
    user.initUserInfo().then((res) => {
      if (res && res.result) {
        commit('storeUserInfo', res.data)
      }
      callback && callback(res)
    })
  },
  doLogin ({commit, state}, params) {
    user.doLogin(params.mobile, params.password)
      .then((res) => {
        user.getUserInfoDao()
        params.resultCallback && params.resultCallback(res)
      })
  },
  doRegiter ({commit, state}, params) {
    user.doRegiter(params.mobile, params.password, params.confirmationCode,
      params.passwordConfirmation)
      .then((res) => {
        user.getUserInfoDao()
        params.resultCallback && params.resultCallback(res)
      })
  },
  doRest ({commit, state}, params) {
    user.doRest(params.mobile, params.password, params.confirmationCode,
      params.passwordConfirmation)
      .then((res) => {
        params.resultCallback && params.resultCallback(res)
      })
  },
  sendCode ({commit, state}, params) {
    user.sendCode(params.mobile)
      .then((res) => {
        params.resultCallback && params.resultCallback(res)
      })
  },
  getUserInfo ({commit, state}, userName) {
    user.getUserInfo(userName).then((res) => {
      if (res && res.result) {
        //todo 其他用户的应该不用保存在state
      }
    })
  },
}

// mutations
const mutations = {
  storeUserInfo (state, userInfo) {
    state.userInfo = userInfo
  },

}

export default {
  state,
  getters,
  actions,
  mutations,
}
